#include "ship.h"
#include "projectile.h"


#include <cmath>

using namespace std;

Ship::Ship(const Vect2D& pos, SDL_Surface* ship_sprite,  World* world) : Object()
{
	position = pos;
	border = 15;
	sprite = ship_sprite;
	speed = Vect2D(0,0);
	acceleration = 1;
	drag = 0.1;
	max_speed = 10;
	health = 50;
	remove = false;

	this->world = world;

	projectile_sprite = NULL;


	direction = 0;
	src_rect.w = 50;
	src_rect.h = 50;
	src_rect.x = 100;
	src_rect.y = 0;
}

void Ship::Move(const int& in_direction)
{
	direction = direction ^ in_direction;
}

void Ship::Frame_calculate()
{
	Vect2D speed_diff = Vect2D(1,0);

	speed_diff = acceleration;
	switch(direction)
	{
		case 9:
			speed_diff.Set_angle(5*M_PI_4);
			src_rect.x = src_rect.w*0;
		break;
		case 1:
			speed_diff.Set_angle(3*M_PI_2);
			src_rect.x = src_rect.w*1;
		break;
		case 3:
			speed_diff.Set_angle(7*M_PI_4);
			src_rect.x = src_rect.w*2;
		break;
		case 2:
			speed_diff.Set_angle(0);
			src_rect.x = src_rect.w*3;
		break;
		case 6:
			speed_diff.Set_angle(M_PI_4);
			src_rect.x = src_rect.w*4;
		break;
		case 4:
			speed_diff.Set_angle(M_PI_2);
			src_rect.x = src_rect.w*5;
		break;
		case 12:
			speed_diff.Set_angle(3*M_PI_4);
			src_rect.x = src_rect.w*6;
		break;
		case 8:
			speed_diff.Set_angle(M_PI);
			src_rect.x = src_rect.w*7;
		break;
		default:
			speed_diff = 0;
		break;

	}


	speed = speed + speed_diff;

	if(speed.Get_lenght() > max_speed)
		speed = max_speed;

	if(speed.Get_lenght() > drag)
		speed = speed.Get_lenght() - drag;
	else
		speed = 0;



}

/********************************
void Draw(...)
Draws the ship and its health meter
on screen.
********************************/
void Ship::Draw(SDL_Surface* screen, const Vect2D& offset)
{
	Vect2D rel_position = position - offset;

	SDL_Rect dest_rect;
	dest_rect.x = rel_position.Get_x() - src_rect.w/2;
	dest_rect.y = rel_position.Get_y() - src_rect.h/2;

	SDL_BlitSurface(sprite, &src_rect, screen, &dest_rect);

	// draw health bar
	SDL_Rect health_bar_rect;

	health_bar_rect.x = rel_position.Get_x() - health/2;
	health_bar_rect.w = health;

	health_bar_rect.y = rel_position.Get_y() - border - 10;
	health_bar_rect.h = 5;

	SDL_FillRect(screen, &health_bar_rect, SDL_MapRGB(screen->format, 0, 200, 0));

}

void Ship::Set_projectile(SDL_Surface* sprite)
{
	projectile_sprite = sprite;
}

void Ship::Shot()
{
	// make sure you have a projectile sprite available
	if(projectile_sprite == NULL)
		return;

	Vect2D projectile_position = Vect2D(1,0);
	Vect2D projectile_speed = Vect2D(1,0);
	int projectile_damage = 5;
	double projectile_size = 5;

	//checks source rectangle to see, which way the ship is pointing
	if(src_rect.x == src_rect.w*1)
	{
		projectile_position.Set_angle(3*M_PI_2);
		projectile_speed.Set_angle(3*M_PI_2);
	}else
	if(src_rect.x == src_rect.w*2)
	{
		projectile_position.Set_angle(7*M_PI_4);
		projectile_speed.Set_angle(7*M_PI_4);
	}else
	if(src_rect.x == src_rect.w*3)
	{
		projectile_position.Set_angle(0);
		projectile_speed.Set_angle(0);
	}else
	if(src_rect.x == src_rect.w*4)
	{
		projectile_position.Set_angle(M_PI_4);
		projectile_speed.Set_angle(M_PI_4);
	}else
	if(src_rect.x == src_rect.w*5)
	{
		projectile_position.Set_angle(M_PI_2);
		projectile_speed.Set_angle(M_PI_2);
	}else
	if(src_rect.x == src_rect.w*6)
	{
		projectile_position.Set_angle(3*M_PI_4);
		projectile_speed.Set_angle(3*M_PI_4);
	}else
	if(src_rect.x == src_rect.w*7)
	{
		projectile_position.Set_angle(M_PI);
		projectile_speed.Set_angle(M_PI);
	}else
	if(src_rect.x == src_rect.w*0)
	{
		projectile_position.Set_angle(5*M_PI_4);
		projectile_speed.Set_angle(5*M_PI_4);
	}

	projectile_position = border + projectile_size + speed.Get_lenght() +1;
	projectile_speed = 50;
	projectile_position = projectile_position + position;
	world->Add_object(new Projectile(projectile_position, projectile_speed, projectile_sprite, projectile_damage, projectile_size));

}

int Ship::Hit()
{
	return 10;
}

void Ship::Set_remove()
{

}
